package com.blogs.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.blogs.entity.CommentQueueFeed;
import com.blogs.entity.SendCommentEntry;
import com.blogs.tools.TbCommentQueue;
import com.blogs.ui.UserLoginActivity;
import com.cnblogs.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class SendCommentService extends Service {
	private static volatile int serviceCount = 0;
	private NotificationManager nm;
	private Notification baseNF;

	private String niceNmae;
	private String commendBody;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i("startId", startId + "");
		if (intent != null) {
			final int is_add_from= intent.getIntExtra("is_add_from", 1);
			
			int type = intent.getIntExtra("type", 0);
			String sid = intent.getStringExtra("sid");
			String comment_id = intent.getStringExtra("comment_id");
			commendBody = intent.getStringExtra("commendBody");
			String parentCommentID = intent.getStringExtra("parentCommentID");
			niceNmae = intent.getStringExtra("niceNmae");
			String blogapp=intent.getStringExtra("blogapp");
			
			final CommentQueueFeed feed=new CommentQueueFeed();
			feed.commendBody=commendBody;
			feed.comment_id=comment_id;
			feed.parentCommentID=parentCommentID;
			feed.type=type;
			feed.blogapp=blogapp;
			
			serviceCount++;
			setNotification(R.drawable.queue_icon_send, "评论发送", "评论发送中");
			new SendComment(is_add_from,type, sid, comment_id, commendBody, parentCommentID,blogapp, new SendComment.IMsgCallBack() {

				public void onSuccess(SendCommentEntry sce) {
					// TODO Auto-generated method stub
					if (sce.IsSuccess == true) {
						setNotification(R.drawable.queue_icon_success, "评论发送", "评论成功");
						Log.i("sendserver", "ok");
						SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd$HH:mm:ss+08:00");

						Intent intent = new Intent("CommentReceiver");
						intent.putExtra("commentId", sce.commentId);
						intent.putExtra("id", sce.Id);
						intent.putExtra("author", niceNmae);
						intent.putExtra("author_url", "");
						intent.putExtra("comment_time", timeFormat.format(new Date()).replace("$", "T"));
						intent.putExtra("content", commendBody);
						sendBroadcast(intent);// 发送广播
					} else {
						if (sce.Message.equals("请先登录")) {
							Intent intent = new Intent();
							intent.setClass(SendCommentService.this, UserLoginActivity.class);
							startActivity(intent);
						} else {

						}
						TbCommentQueue tb=new TbCommentQueue(SendCommentService.this);
						feed.err_text=sce.Message;
						tb.Insert(feed);
						tb.DbClose();
						Intent intent = new Intent("CommentQueueReceiver");
						sendBroadcast(intent);// 发送评论失败广播
						
						setNotification(R.drawable.queue_icon_miss, "评论失败", "评论失败," + sce.Message);
						Log.i("sendserver", "faile");
					}
					serviceCount--;
					if (serviceCount == 0)
						stopTheServer();
				}

				public void onFailed() {
					// TODO Auto-generated method stub
					setNotification(R.drawable.queue_icon_miss, "评论发送", "评论失败");
					TbCommentQueue tb=new TbCommentQueue(SendCommentService.this);
					feed.err_text="网络异常";
					tb.Insert(feed);
					tb.DbClose();
					Intent intent = new Intent("CommentQueueReceiver");
					sendBroadcast(intent);// 发送评论失败广播
					Log.i("sendserver", "onFailed");
					serviceCount--;
					if (serviceCount == 0)
						stopTheServer();
				}
			}).Send();
		}
		return super.onStartCommand(intent, flags, startId);
	}

	private void setNotification(int icon, String title, String con) {
		baseNF = new Notification(icon, con, System.currentTimeMillis());
		// baseNF=new Notification();
		// baseNF.icon=R.drawable.queue_icon_send;
		// baseNF.tickerText="评论发送中";
		// baseNF.when=System.currentTimeMillis();
		baseNF.flags = Notification.FLAG_AUTO_CANCEL;
		Intent nintent = new Intent();
		nintent.setAction(Intent.ACTION_MAIN);
		nintent.addCategory(Intent.CATEGORY_LAUNCHER);
		nintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		nintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pendIntent = PendingIntent.getActivity(SendCommentService.this, 0, nintent, 0);
		baseNF.setLatestEventInfo(SendCommentService.this, title, con, pendIntent);
		nm.notify(R.string.app_name, baseNF);
		nm.cancelAll();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void stopTheServer() {
		this.stopSelf();
	}
}
